<?php

class Mysql
{
    public $connect;             //数据库连接对象
    public $config;         //数据库配置数据
    public $server;         //服务器连接地址
    public $username;       //数据库用户名
    public $password;       //数据库密码
    public $sql;            //sql语句变量
    public $defaultDatabase; //默认设置的数据库
    public $limitcount;     //限制的数量
    public $message = array();


    public function __construct()
    {
        $this->server = "localhost";
        $this->port = "3306";
        $this->username = "root";
        $this->password = "yx110120";
        $this->dbName = "system";
    }
    public function _destruct()
    {
        $this->server = '';
        $this->username = '';
        $this->password = '';
        $this->port = '';
        $this->defaultDatabase = '';
    }
    public function dbConnect()
    {
        $this->connect = mysqli_connect($this->server, $this->username, $this->password, $this->dbName);
    }

    //查询所有数据
    //$table:String,$data:String
    public function selectAllData($table, $data = "*")
    {
        $this->dbConnect();
        $query = mysqli_query($this->connect, "select ${data} from ${table}");
        if (!$query) {
            $this->message = array("code" => 500, "msg" => "服务器异常", "data" => null);
            return $this->message;
        };
        $query_data = array();
        while ($item = mysqli_fetch_assoc($query)) {
            $query_data[] = $item;
        }
        $this->message = array("code" => 0, "msg" => "查询成功", "data" => $query_data);
        return $this->message;
    }

    public function selectDataByPage($table, $start, $pageSize, $date = "date")
    {
        $this->dbConnect();
        $count = mysqli_query($this->connect, "select count(id) as count  from ${table};");
        if (!$count) {
            $this->message = array("code" => 500, "msg" => "服务器异常", "data" => null);
        } else {
            $query = mysqli_query($this->connect, "select *  from ${table} order by ${date} desc limit {$start},{$pageSize};");
            if (!$query) {
                $this->message = array("code" => 500, "msg" => "服务器异常", "data" => null);
            } else {
                $query_data = array();
                while ($item = mysqli_fetch_assoc($query)) {
                    $query_data[] = $item;
                }
                $count = (int) mysqli_fetch_assoc($count)["count"];
                $this->message = array("code" => 0, "msg" => "查询成功", "data" => $query_data, "num" => $count);
            }
        }
        return $this->message;
    }

    //查询部分数据
    //$sqlData:String,$sqlCount:String
    public function selectPageData($sqlData = null, $sqlCount = null)
    {
        $this->dbConnect();
        if ($sqlCount != null && $sqlData == null) {
            $count = mysqli_query($this->connect, $sqlCount);
            if (!$count) {
                $this->message = array("code" => 500, "msg" => "服务器异常", "data" => null);
                return $this->message;
            };
            $count = (int) mysqli_fetch_assoc($count)["count"];
            $this->message = array("code" => 0, "msg" => "查询成功", "num" => $count);
            return $this->message;
        } else if ($sqlData != null && $sqlCount == null) {
            $query = mysqli_query($this->connect, $sqlData);
            if (!$query) {
                $this->message = array("code" => 500, "msg" => "服务器异常", "data" => null);
                return $this->message;
            };
            $query_data = array();
            while ($item = mysqli_fetch_assoc($query)) {
                $query_data[] = $item;
            }
            $this->message = array("code" => 0, "msg" => "查询成功", "data" => $query_data);
            return $this->message;
        } else if ($sqlData != null && $sqlCount != null) {
            $count = mysqli_query($this->connect, $sqlCount);
            if (!$count) {
                $this->message = array("code" => 500, "msg" => "服务器异常", "data" => null);
                return $this->message;
            };
            $query = mysqli_query($this->connect, $sqlData);
            if (!$query) {
                $this->message = array("code" => 500, "msg" => "服务器异常", "data" => null);
                return $this->message;
            };
            $query_data = array();
            while ($item = mysqli_fetch_assoc($query)) {
                $query_data[] = $item;
            }
            $count = (int) mysqli_fetch_assoc($count)["count"];
            $this->message = array("code" => 0, "msg" => "查询成功", "data" => $query_data, "num" => $count);
            return $this->message;
        } else {
            $this->message = array("code" => 500, "msg" => "服务器异常", "data" => null);
            return $this->message;
        }
    }
    //添加或修改数据
    //$sql:String
    public function addOrEditData($sql)
    {
        $this->dbConnect();
        $query = mysqli_query($this->connect, $sql);
        if (!$query)
            $this->message = array("code" => 300, "msg" => "任务失败");
        else
            $this->message = array("code" => 0, "msg" => "任务成功");

        return $this->message;
    }

    //通过ID删除数据
    //$table:String,$id:String|Number
    public function deleteDataById($table, $id)
    {
        $this->dbConnect();
        if (empty($id)) {
            $this->message = array("code" => 400, "msg" => "格式错误");
        } else {
            $query = mysqli_query($this->connect, "delete from ${table} where id=${id};");
            if (!$query)
                $this->message = array("code" => 500, "msg" => "服务器异常", "data" => null);
            else
                $this->message = array("code" => 0, "msg" => "删除成功");
        }
        return $this->message;
    }
}
